﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using anaii.EasyNews.Data;
using DotNetNuke.Entities.Modules;
using System.Threading;
using DotNetNuke.Services.FileSystem;
using DotNetNuke.Common;


namespace anaii.EasyNews.Controller
{
    public class EasyNewsController: ISearchable,IPortable
    {

        #region ISearchable Members

        public DotNetNuke.Services.Search.SearchItemInfoCollection GetSearchItems(ModuleInfo ModInfo)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IPortable Members

        public string ExportModule(int ModuleID)
        {
            throw new NotImplementedException();
        }

        public void ImportModule(int ModuleID, string Content, string Version, int UserID)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region Public Utils

        public static string GetTranslation(EasyNew currentnews, string property)
        {
            string retVal = "";
            EasyNewsDBDataContext db = new EasyNewsDBDataContext();
            dnn_EasyNewsTranslation translation = currentnews.dnn_EasyNewsTranslations.FirstOrDefault(p => p.Culture == Thread.CurrentThread.CurrentCulture.Name);
            if (translation == null)
                translation = currentnews.dnn_EasyNewsTranslations.FirstOrDefault(p => p.Culture == "en-US");
            if (translation == null)
                translation = currentnews.dnn_EasyNewsTranslations.FirstOrDefault();

            dnn_EasyNewsTypesTranslation translationType = currentnews.EasyNewsType.dnn_EasyNewsTypesTranslations.FirstOrDefault(p => p.Culture == Thread.CurrentThread.CurrentCulture.Name);
            if (translationType == null)
                translationType = currentnews.EasyNewsType.dnn_EasyNewsTypesTranslations.FirstOrDefault(p => p.Culture == "en-US");
            if (translationType == null)
                translationType = currentnews.EasyNewsType.dnn_EasyNewsTypesTranslations.FirstOrDefault();

            if (property.ToLower() == "title")
            {
                if (translation != null)
                    retVal = translation.Title;
            }
            else if (property.ToLower() == "shortdescription")
            {
                if (translation != null)
                    retVal = translation.ShortDescription;
            }
            else if (property.ToLower() == "fulldescription1")
            {
                if (translation != null)
                    retVal = translation.FullDescription1;
            }
            else if (property.ToLower() == "fulldescription2")
            {
                if (translation != null)
                    retVal = translation.FullDescription2;
            }
            else if (property.ToLower() == "typename")
            {
                if (translationType != null)
                    retVal = translationType.TypeName;
            }

            return retVal;
        }

        public static string ReplaceTokens(string template, EasyNew currentnews)
        {
            if (template == null)
                template = "";
            if (currentnews.NewsID <= 0)
                return template;

            string retVal = template;

            //...
            retVal = retVal.Replace("[NewsIDTT]", currentnews.NewsID.ToString());
            retVal = retVal.Replace("[TitleTT]", "<span>" + GetTranslation(currentnews, "Title") + "</span>");
            retVal = retVal.Replace("[SmallDescTT]", "<span>" + GetTranslation(currentnews, "ShortDescription") + "</span>");
            retVal = retVal.Replace("[FullDesc1TT]", GetTranslation(currentnews, "FullDescription1"));
            retVal = retVal.Replace("[FullDesc2TT]", GetTranslation(currentnews, "FullDescription2"));
            retVal = retVal.Replace("[NewsTypeTT]", "<span>" + GetTranslation(currentnews, "TypeName") + "</span>");
            retVal = retVal.Replace("[DateTT]", "<span>" + currentnews.DateCreated.Date.ToShortDateString() + "</span>");
            retVal = retVal.Replace("[DateLongTT]", "<span>" + currentnews.DateCreated.Date.ToString("dd MMMM yyyy") + "</span>");
            string imageURL = "";
            string imageAlt = "";
            if (!string.IsNullOrEmpty(currentnews.ImageURL) && currentnews.ImageURL.StartsWith("FileID="))
            {
                FileController fileCnt = new FileController();
                FileInfo objFile = fileCnt.GetFileById(int.Parse(currentnews.ImageURL.Substring(7)), Globals.GetPortalSettings().PortalId);
                if (objFile != null)
                {
                    imageURL = Globals.GetPortalSettings().HomeDirectory + objFile.Folder + objFile.FileName;
                    imageAlt = objFile.FileName.Replace(objFile.Extension,"");
                }
            }
            retVal = retVal.Replace("[ImageTT]", "<img class=\"singleNewsImageCss\" src=\"" + imageURL + "\" alt=\"" + imageAlt + "\" >");
            string thumbURL = "";
            string thumbAlt = "";
            if (!string.IsNullOrEmpty(currentnews.ThumbURL) && currentnews.ThumbURL.StartsWith("FileID="))
            {
                FileController fileCnt = new FileController();
                FileInfo objFile = fileCnt.GetFileById(int.Parse(currentnews.ThumbURL.Substring(7)), Globals.GetPortalSettings().PortalId);
                if (objFile != null)
                {
                    thumbURL = Globals.GetPortalSettings().HomeDirectory + objFile.Folder + objFile.FileName;
                    thumbAlt = objFile.FileName.Replace(objFile.Extension, "");
                }
            }
            retVal = retVal.Replace("[ThumbnailTT]", "<img class=\"singleNewsThumbnailCss\" src=\"" + thumbURL + "\" alt=\"" + thumbAlt + "\" >");
            //...

            return retVal;
        }

        #endregion
    }
}
